package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goal;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class GoalDao {
	
	private DBHelp<Goal> db = new DBHelp<Goal>();
	
	public void Save (String id,String name,String desc,String createtime,String t_project_id,String t_employee_id) {
		String sql = "insert into t_goal(id,name,desc,createtime,t_project_id,t_employee_id) values(?,?,?,?,?,?)";
		db.executeSQL(sql, id,name,desc,createtime,t_project_id,t_employee_id);
	}

	public void Del (String id) {
		String sql = "delete from t_goal where id = ?";
		db.executeSQL(sql, id);
	}
	
	public void Update (Goal goal) {
		String sql = "update t_goal set name = ?,desc = ?,createtime = ?,t_project_id = ?,t_employee_id = ? where id = ?";
		db.executeSQL(sql,goal.getName(),goal.getDesc(),goal.getCreatetime(),goal.getT_project_id(),goal.getT_employee_id(),goal.getId());
	}
	
	public Goal FindById (String id) {
		String sql = "select name,desc,createtime,t_project_id,t_employee_id from t_goal where id = ?";
		return db.executeQueryForObject(sql, new GoalRowMapper(), id);
	}
	
	public List<Goal> FindAll () {
		String sql = "select name,desc,createtime,t_project_id,t_employee_id from t_goal";
		return db.executeQueryForList(sql, new GoalRowMapper());
	}
	
	private class GoalRowMapper implements RowMapper<Goal> {
		
		public Goal mapperRow (ResultSet rs) throws SQLException {
			
			Goal goal = new Goal();
			goal.setId(rs.getString("id"));
			goal.setName(rs.getString("name"));
			goal.setDesc(rs.getString("desc"));
			goal.setCreatetime(rs.getString("createtime"));
			goal.setT_project_id(rs.getString("t_project_id"));
			goal.setT_employee_id(rs.getString("t_employee_id"));
			return goal;
		}
	}
}
